﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LIMS.SqlServerDAL
{
    public class NewsInfoService : LIMS.IDAL.INewsInfo
    {
        LIMSDataContext DB = new LIMSDataContext();
        /// <summary>
        /// 添加新闻
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public Boolean Add(LIMS.Model.NewsInfos info)
        {
            TB_Info infoTemp = new TB_Info()
            {
                infotitle = info.newsInfoTitle,
                infoType = info.newsInfoType,
                infoDetail = info.newsInfoDetail
            };
            DB.TB_Info.InsertOnSubmit(infoTemp);
            DB.SubmitChanges();
            return true;
        }

        /// <summary>
        /// 获取所有新闻
        /// </summary>
        /// <returns></returns>
        public List<LIMS.Model.NewsInfos> FindAll()
        {
            List<Model.NewsInfos> listInfo = new List<Model.NewsInfos>();
            DB.TB_Info.Select(s => s).ToList().ForEach((item) =>
            {
                Model.NewsInfos info = new Model.NewsInfos()
                {
                    newsInfoID = item.infoId,
                    newsInfoTitle = item.infotitle,
                    newsInfoType = (item.infoType.HasValue == true) ? item.infoType.Value : false,
                    newsInfoDetail = item.infoDetail
                };
                listInfo.Add(info);
            });
            return listInfo;
        }

        /// <summary>
        /// 按新闻类别查询
        /// </summary>
        /// <param name="blInfoType"></param>
        /// <returns></returns>
        public List<LIMS.Model.NewsInfos> FindByType(Boolean infoType)
        {
            List<Model.NewsInfos> listInfo = new List<Model.NewsInfos>();
            DB.TB_Info.Where(w => (w.infoType.HasValue ? w.infoType.Value : false) == infoType).Select(s => s).ToList().ForEach((item) =>
            {
                Model.NewsInfos info = new Model.NewsInfos()
                {
                    newsInfoID = item.infoId,
                    newsInfoTitle = item.infotitle,
                    newsInfoType = (item.infoType.HasValue == true) ? item.infoType.Value : false,
                    newsInfoDetail = item.infoDetail
                };
                listInfo.Add(info);
            });
            return listInfo;
        }

        /// <summary>
        /// 根据新闻标题模糊查询数据
        /// </summary>
        /// <param name="newsTitle"></param>
        /// <returns></returns>
        public List<LIMS.Model.NewsInfos> FindByTitle(String newsTitle)
        {
            List<Model.NewsInfos> listInfo = new List<Model.NewsInfos>();
            DB.TB_Info.Where(w => w.infotitle.Contains(newsTitle)).Select(s => s).ToList().ForEach((item) =>
            {
                Model.NewsInfos info = new Model.NewsInfos()
                {
                    newsInfoID = item.infoId,
                    newsInfoTitle = item.infotitle,
                    newsInfoType = (item.infoType.HasValue == true) ? item.infoType.Value : false,
                    newsInfoDetail = item.infoDetail
                };
                listInfo.Add(info);
            });
            return listInfo;
        }

        /// <summary>
        /// 更新单条新闻数据
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public Boolean Modify(LIMS.Model.NewsInfos info)
        {
            var infoTemp = DB.TB_Info.SingleOrDefault(sg => sg.infoId == info.newsInfoID);
            infoTemp.infoDetail = info.newsInfoDetail;
            infoTemp.infotitle = info.newsInfoTitle;
            infoTemp.infoType = info.newsInfoType;
            DB.SubmitChanges();
            return true;
        }

        /// <summary>
        /// 获取新闻综条数
        /// </summary>
        /// <returns></returns>
        public Int32 Count()
        {
            return DB.TB_Info.Count();
        }

    }
}
